package com.example.doer.reading.AppActivities;


import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;

import com.example.doer.reading.MyReadTool.ReadActivity;
import com.example.doer.reading.MyReadTool.adapter.ShelfAdapter;
import com.example.doer.reading.MyReadTool.db.BookList;
import com.example.doer.reading.MyReadTool.filechooser.FileChooserActivity;
import com.example.doer.reading.MyReadTool.view.DragGridView;
import com.example.doer.reading.R;
import com.example.doer.reading.myHome.layout.SmartRefreshLayout;
import com.example.doer.reading.myHome.layout.api.RefreshLayout;
import com.example.doer.reading.myHome.layout.listener.OnRefreshListener;

import org.litepal.crud.DataSupport;

import java.io.File;
import java.util.List;

import static com.ab.util.AbToastUtil.showToast;


public class BookListFragment extends Fragment {
    private List<BookList> bookLists;
    private ShelfAdapter adapter;
    private int itemPosition;
    private SmartRefreshLayout smartRefreshLayout;
    public BookListFragment() {
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_book_list, container, false);
        Button input=(Button)view.findViewById(R.id.input_book_btn);
        final DragGridView bookShelf= (DragGridView) view.findViewById(R.id.bookShelf);
        smartRefreshLayout= (SmartRefreshLayout) view.findViewById(R.id.refreshLayout);
        bookLists = DataSupport.findAll(BookList.class);
        adapter = new ShelfAdapter(getActivity(),bookLists);
        bookShelf.setAdapter(adapter);

        smartRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                bookLists = DataSupport.findAll(BookList.class);
                adapter = new ShelfAdapter(getActivity(),bookLists);
                bookShelf.setAdapter(adapter);
                smartRefreshLayout.finishRefresh();
            }
        });



        bookShelf.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (bookLists.size() > position) {
                    itemPosition = position;
                    String bookname = bookLists.get(itemPosition).getBookname();

                    adapter.setItemToFirst(itemPosition);
//                bookLists = DataSupport.findAll(BookList.class);
                    final BookList bookList = bookLists.get(itemPosition);
                    bookList.setId(bookLists.get(0).getId());
                    if (bookList.isIfLocal()){
                        final String path = bookList.getBookpath();
                        File file = new File(path);
                        if (!file.exists()) {
                            new AlertDialog.Builder(getActivity())
                                    .setTitle(getActivity().getString(R.string.app_name))
                                    .setMessage(path + "文件不存在,是否删除该书本？")
                                    .setPositiveButton("删除", new DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface dialog, int which) {
                                            DataSupport.deleteAll(BookList.class, "bookpath = ?", path);
                                            bookLists = DataSupport.findAll(BookList.class);
                                            adapter.setBookList(bookLists);
                                        }
                                    }).setCancelable(true).show();
                            return;
                        }
                        ReadActivity.openBook(bookList, getActivity());
                    }else {
                        showToast(getActivity(),"请先下载书籍");
                    }
                }
            }
        });

        input.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(),FileChooserActivity.class);
                getActivity().startActivity(intent);
            }
        });
        return view;
    }
}


